﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

public class MissionsScrollView : MonoBehaviour
{
    public ScrollRect scroll;

    public ToggleGroup toggleGroup; 

    public Vector2 cellSize;

    public Vector2 spacing;

    public int rowCount;

    private Rect viewportRect;

    private int _missionNum;

    public GameObject prefab;
    public List<MissionItem> listRenders;
    private int renderCount;

    public UnityEvent<MissionItem> onValueChange;

    private void Awake()
    {
        listRenders = new List<MissionItem>();
        prefab.SetActive(false);

        renderCount = rowCount +1;
        float w = cellSize.x;
        float h = cellSize.y * rowCount + spacing.y * (rowCount - 1);
        viewportRect = new Rect(0, -h, w, h);    

        scroll.onValueChanged.AddListener(ScrollValueChangedHanlder);
    }

    private void Start()
    {
        for (int i = 0; i < renderCount; i++)
        {
            var obj = Instantiate(prefab, scroll.content.transform);
            listRenders.Add(new MissionItem(i, obj.transform as RectTransform));
            listRenders[i].transform.anchoredPosition = GetItemPosition(i);
            obj.GetComponent<Toggle>().group = toggleGroup;
            obj.SetActive(true);
            if (onValueChange != null && i < _missionNum)
            {
                onValueChange.Invoke(listRenders[i]);
            }
        }
    }

    private void ScrollValueChangedHanlder(Vector2 arg0)
    {
        Vector2 p0 = scroll.content.anchoredPosition;
        for (int i = 0; i < listRenders.Count; i++)
        {
            //获得格子相对视窗的坐标
            Vector2 p = p0 + listRenders[i].transform.anchoredPosition;

            if (p.y - cellSize.y > viewportRect.yMax)//如果格子的底部高出了视窗顶部，则把格子移动到后面
            {
                if (listRenders[i].index + renderCount < _missionNum)//判断是否到末尾
                {
                    listRenders[i].index += renderCount;
                    listRenders[i].transform.anchoredPosition = GetItemPosition(listRenders[i].index);
                    if (onValueChange != null && listRenders[i].index < _missionNum)
                    {
                        onValueChange.Invoke(listRenders[i]);
                    }
                }

            }
            else if (p.y < viewportRect.yMin)//如果格子的顶部低出了视窗底部，则把格子移动到前面
            {
                if (listRenders[i].index - renderCount >= 0)//判断是否到开头
                {
                    listRenders[i].index -= renderCount;
                    listRenders[i].transform.anchoredPosition = GetItemPosition(listRenders[i].index);
                    if (onValueChange != null && listRenders[i].index < _missionNum)
                    {
                        onValueChange.Invoke(listRenders[i]);
                    }
                }
            }
        }
    }

    public void SetCount(int v)
    {
        if (_missionNum != v)
        {
            int lastCount = _missionNum;
            _missionNum = v;

            RefreshRenders();

            //计算scroll.content的高度，保证正常的拖拽
            scroll.content.sizeDelta = new Vector2(scroll.content.sizeDelta.x, cellSize.y * rowCount + spacing.y * (rowCount - 1));
        }
        else//如果道具数量没变，则刷新道具显示
        {
            RefreshRenders();
        }
    }

    private void RefreshRenders()
    {
        if (listRenders == null)
            return;

        for (int i = 0; i < listRenders.Count; i++)
        {
            if (listRenders[i].index < _missionNum)
            {
                if (onValueChange != null)
                {
                    onValueChange.Invoke(listRenders[i]);
                }
                listRenders[i].transform.gameObject.SetActive(true);
            }
            else
            {
                listRenders[i].transform.gameObject.SetActive(false);
            }
        }
    }

    public Vector2 GetItemPosition(int index)
    {
        return new Vector2(spacing.x, -index * (cellSize.y + spacing.y));
    }
}
